package cn.chinasyq.photoquan.photo;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;

import cn.chinasyq.photoquan.PreviewNetworkPhotoActivity;
import cn.chinasyq.photoquan.R;
import cn.chinasyq.photoquan.photo.mode.PhotoMode;
import cn.chinasyq.photoquan.user.activity.LoginActivity;
import cn.chinasyq.photoquan.util.BitmapUtil;
import cn.chinasyq.photoquan.util.CameraHead;
import cn.chinasyq.photoquan.util.DialogUtil;
import cn.master.util.utils.RequestCodeUtil;
import cn.master.util.utils.ToastUtil;
import cn.master.volley.models.response.handler.ResponseHandler;

public class SendCommentActivity extends Activity implements OnClickListener, ResponseHandler.OnNeedLoginListener, ResponseHandler.OnSucceedListener, ResponseHandler.OnFailedListener {

    private static final String TAG_COMMENT = "comment";
    private EditText et_content;
    private String uuid;
    private ImageButton add;
    private Uri image;
    private String type;
    private Dialog dialog;
    private ArrayList<String> urlList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upload_comment);
        uuid = getIntent().getStringExtra(PhotoCommentActivity.UUID);
        type = getIntent().getStringExtra(PhotoCommentActivity.TYPE);
        initView();

    }

    public void initView() {
        et_content = (EditText) findViewById(R.id.et_content);
        add = (ImageButton) findViewById(R.id.add);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.add:
                if (v.getTag() == null) {
                    CameraHead.selectModeDialog(SendCommentActivity.this);
                } else {
                    Intent intent = new Intent(getApplicationContext(), PreviewNetworkPhotoActivity.class);
                    urlList = new ArrayList<>();
                    urlList.add(((Uri) v.getTag()).toString());
                    intent.putExtra(PreviewNetworkPhotoActivity.INTENT_FILE_PATH,
                            urlList);
                    intent.putExtra(PreviewNetworkPhotoActivity.INTENT_POSITION,
                            0);
                    intent.putExtra(PreviewNetworkPhotoActivity.MODE,PreviewNetworkPhotoActivity.MODE_EDIT);
                    startActivityForResult(intent, RequestCodeUtil.getInstance().obtainRequestCode(PreviewNetworkPhotoActivity.class));
                }
                break;
            case R.id.btn_commit:
                sendComment(et_content.getText().toString(), image);
                break;
            case R.id.ibtn_back:
                onBackPressed();
                break;
        }
    }

    protected void sendComment(String comment, Uri image) {
        if (null == dialog)
            dialog = DialogUtil.createProgressDialog(this);
        dialog.show();
        ResponseHandler handler = new ResponseHandler(TAG_COMMENT);
        handler.setOnSucceedListener(this);
        handler.setOnFailedListener(this);
        handler.setOnNeedLoginListener(this);
        PhotoMode.sendComment(getApplicationContext(), handler, uuid, comment, image, type);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RequestCodeUtil.getInstance().obtainRequestCode(PreviewNetworkPhotoActivity.class)) {
            if (resultCode == RESULT_OK) {
                image = null;
                add.setImageResource(R.mipmap.add);
                add.setTag(null);
            }
        } else {
            try {
                image = CameraHead.callbackZoom(SendCommentActivity.this, requestCode, resultCode, data, 280, 280);
                if (image != null) {
                    BitmapUtil.compressBitmap(BitmapUtil.convertUri2FilePath(this, image), 100);
                    add.setTag(image);
                    add.setImageURI(image);
                }
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onNeedLogin(String tag) {
        startActivity(new Intent(this, LoginActivity.class));
    }

    @Override
    public void onSucceed(String tag, boolean isCache, Object data) {
        setResult(RESULT_OK);
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
        finish();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        setResult(RESULT_CANCELED);
    }

    @Override
    public void onFailed(String tag, ResponseHandler.FailedStatus status, int resultCode, String msg) {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
        if (msg == null) {
            switch (status) {
                case DATA_ERROR:
                    ToastUtil.show(getApplicationContext(), R.string.tip_data_error);
                    break;
                case NETWORK_ERROR:
                    ToastUtil.show(getApplicationContext(), R.string.servererrortips);
                    break;
            }
        } else {
            ToastUtil.show(getApplicationContext(), msg);
        }
    }
}
